/**
 * 用户个人信息更新接口测试用例
 *
 * Created by snowingsea on 2020/02/03.
 */
const randomstring = require('randomstring');
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));

const common = require('./../common');
const Administrator = require('./../../models/administrator');
const uri = require('./../uri');

module.exports = describe('/PUT me.json', function () {
  const url = '/me.json';
  const administrator = new Administrator();
  const newUserIds = [];
  let user = {};

  before(function (done) {
    administrator.createRandomUser()
      .then((res) => {
        user = res;
        newUserIds.push(user.userId);
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'put');

  // 正常请求
  [
    { nickname: randomstring.generate(16) },
    { lang: randomstring.generate(6) },
    { nickname: randomstring.generate(16), lang: randomstring.generate(6) },
  ].forEach((edit, index) => {
    it(`it should ${index === 0 ? 'PUT' : 'put'} my information with ${JSON.stringify(edit)}`, function (done) {
      user.login()
        .then(() => {
          chai.request(uri)
            .put(url)
            .set('Authorization', `Bearer ${user.token.accessToken}`)
            .query(edit)
            .end((err, res) => {
              res.should.have.status(200);
              common.should.be.userMessage(res.body);
              res.body.userId.should.be.eql(user.userId);
              Object.keys(edit).forEach((key) => {
                res.body[key].should.be.eql(edit[key]);
              });
              done();
            });
        })
        .catch(done);
    });
  });
});
